﻿using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DataAccess;
using Model.EntityModel;

namespace Project
{
    /// <summary>
    /// Interaction logic for ViewCopmlaintContent.xaml
    /// </summary>
    public partial class ViewComplaintContent : UserControl
    {
        public long ComplaintId { get; set; }
        public bool wasError;
        public ViewComplaintPart2 viewComplaintpart2 { get; set; }
        public ViewComplaintContent(long complaintId)
        {
            InitializeComponent();
            ComplaintId = complaintId;
            try
            {
                this.Loaded += loaded;
                lblCaseNumber.Content = ComplaintId;
                SetComplainantInfo();
                SetAggressorInfo();
                SetVictimInfo();
                SetComplaintInfo();
            }
            catch(Exception)
            {
                wasError = true;
                Xceed.Wpf.Toolkit.MessageBox.Show("Error esta denuncia no esta completada, solo tiene accion directa!", "Error!",MessageBoxButton.OK,MessageBoxImage.Error);
            }
        }

        private void loaded(object sender, RoutedEventArgs e)
        {
            if(wasError)
                UIWindowManager.CleanWindow(this);
        }

        private void SetComplainantInfo()
        {
            Actor actorComplainant = PersonAndActorDataAccess.GetActorByTypeAndComplaintId("Denunciante", ComplaintId);
            var personComplainant = PersonAndActorDataAccess.GetPersonById(actorComplainant.IdPerson);
            tbCompName.Content = personComplainant.Name;
            tbCompLastName.Content = personComplainant.PaternalName;
            tbCompMiddleName.Content = personComplainant.MaternalName;
            CompBornDate.Content = personComplainant.BirthDate.Value.ToShortDateString();
            lblCompGender.Content = (personComplainant.Gender[0] == 'M')?"Varón":"Mujer";
            tbCompCI.Content = personComplainant.CiPassport.ToString();
            tbCompPhone.Content = personComplainant.Phone;
            cbCompCivilStatus.Content = personComplainant.CivilStatus;
            tbCompAddress.Content = personComplainant.Address;
            cboxCompCountry.Content = personComplainant.Nationality;
            tbCompZone.Content = personComplainant.Zone;
            tbOcc.Content = personComplainant.Ocupation;
        }

        private void SetAggressorInfo()
        {
            Actor actorAggressor = PersonAndActorDataAccess.GetActorByTypeAndComplaintId("Agresor", ComplaintId);
            var personAggressor = PersonAndActorDataAccess.GetPersonById(actorAggressor.IdPerson);
            tbAggressorCi.Content = personAggressor.CiPassport.ToString();
            tbAggressorName.Content = personAggressor.Name;
            tbAggressorLastName.Content = personAggressor.PaternalName;
            tbAggressorMiddleName.Content = personAggressor.MaternalName;
            AggressorBornDate.Content = personAggressor.BirthDate.Value.ToShortDateString();
            lblAggressorGender.Content = (personAggressor.Gender[0] == 'M') ? "Varón" : "Mujer";
            tbAggressorPhone.Content = personAggressor.Phone;
            cbAggressorCivilStatus.Content = personAggressor.CivilStatus;
            cbAggressorCountry.Content = personAggressor.Nationality;
            tbAggressorBornPlace.Content = personAggressor.BornCity + " (Area " + personAggressor.BornArea + ")";
            cbAggressorInstructionLevel.Content = personAggressor.LevelOfEducation;
            lblAggressorActivityCond.Content = personAggressor.ActivityStatus;
            //temperancia de denuncia
            AggressorImage.Source = new BitmapImage(new Uri(PhotoAlbumDataAccess.GetActorImagePath(ComplaintId, "Agresor")));
            TooltipAggressorImage.Source = AggressorImage.Source;
            VictimImage.Source = new BitmapImage(new Uri(PhotoAlbumDataAccess.GetActorImagePath(ComplaintId, "Victima")));
            TooltipVictimImage.Source = VictimImage.Source;
        }

        private void SetVictimInfo()
        {
            Actor actorVictim = PersonAndActorDataAccess.GetActorByTypeAndComplaintId("Victima", ComplaintId);
            var personVictim = PersonAndActorDataAccess.GetPersonById(actorVictim.IdPerson);
            tbVictimName.Content = personVictim.Name;
            tbVictimLastName.Content = personVictim.PaternalName;
            tbVictimMiddleName.Content = personVictim.MaternalName;
            VictimBornDate.Content = personVictim.BirthDate.Value.ToShortDateString();
            lblVictimGender.Content = (personVictim.Gender[0] == 'M') ? "Varón" : "Mujer";
            tbVictimCI.Content = personVictim.CiPassport.ToString();
            tbVictimPhone.Content = personVictim.Phone;
            cbVictimCivilStatus.Content = personVictim.CivilStatus;
            tbVictimBornPlace.Content = personVictim.BornCity + " (Area " + personVictim.BornArea + ")";
            cbVictimCountry.Content = personVictim.Nationality;
            cbVictimInstructionLevel.Content = personVictim.LevelOfEducation;
            lblVictimActivityCond.Content = personVictim.ActivityStatus;
        }

        private void SetComplaintInfo()
        {
            var complaint = ComplaintDataAccess.GetComplaintById(ComplaintId);
            lblNatureOfFact.Content = complaint.IncidentNature;
            lblIncidentPlace.Content = complaint.IncidentPlace;
            lblIncidentZone.Content = complaint.IncidentZone;
            lblCaseDate.Content = complaint.RegistrationDate.ToShortDateString();
            dateIncident.Content = complaint.IncidentDate.Value.ToShortDateString();
            cboxCompPartnership.Content = complaint.ComplainantVictimRelationship;
            cboxVictimPartnership.Content = complaint.VictimAgressorRelationship;
            lblAggressorTemp.Content = complaint.ConductOfAgressor;
            SetViolenceCauses(complaint);
            SetViolenceForms(complaint);
            SetMultidisciplinaryTeam(complaint);
            SetCaseDerivations(complaint);
            if (complaint.CasesAttention == "Citacion Y Arresto")
            {
                chkArrest.IsChecked = true;
                chkMeeting.IsChecked = true;
            }
            if (complaint.CasesAttention == "Arresto")
            {
                chkArrest.IsChecked = true;
            }
            if (complaint.CasesAttention == "Citacion")
            {
                chkMeeting.IsChecked = true;
            }
        }

        private void SetViolenceCauses(Complaint complaint)
        {
            foreach (var violenceCause in complaint.ViolenceCause)
            {
                if (chkEconomics.Content.ToString() == violenceCause.Cause)
                    chkEconomics.IsChecked = true;
                if (chkFamiliar.Content.ToString() == violenceCause.Cause)
                    chkFamiliar.IsChecked = true;
                if (chkJealous.Content.ToString() == violenceCause.Cause)
                    chkJealous.IsChecked = true;
                if (chkInfidelity.Content.ToString() == violenceCause.Cause)
                    chkInfidelity.IsChecked = true;
                if (chkOthersNature.Content.ToString() == violenceCause.Cause)
                    chkOthersNature.IsChecked = true;
                if (chkDomainHabits.Content.ToString() == violenceCause.Cause)
                    chkDomainHabits.IsChecked = true;
            }
        }

        private void SetViolenceForms(Complaint complaint)
        {
            foreach (var violenceForm in complaint.ViolenceForm)
            {
                if (chkPhysics.Content.ToString() == violenceForm.Form)
                    chkPhysics.IsChecked = true;
                if (chkPsicologics.Content.ToString() == violenceForm.Form)
                    chkPsicologics.IsChecked = true;
                if (chkSexual.Content.ToString() == violenceForm.Form)
                    chkSexual.IsChecked = true;
                if (chkOthersViolenceForm.Content.ToString() == violenceForm.Form)
                    chkOthersViolenceForm.IsChecked = true;
            }
            var firstOrDefault = complaint.ViolenceForm.FirstOrDefault();
            if (firstOrDefault != null)
                tbDescription.Content = firstOrDefault.Description;
        }

        private void SetMultidisciplinaryTeam(Complaint complaint)
        {
            foreach (var multiTeam in complaint.MultidisciplinaryTeam)
            {
                if (chkMedic.Content.ToString() == multiTeam.Professional)
                    chkMedic.IsChecked = true;
                if (chkPsicology.Content.ToString() == multiTeam.Professional)
                    chkPsicology.IsChecked = true;
                if (chkSocialWorker.Content.ToString() == multiTeam.Professional)
                    chkSocialWorker.IsChecked = true;
                if (chkLegalAsessor.Content.ToString() == multiTeam.Professional)
                    chkLegalAsessor.IsChecked = true;
            }
        }

        private void SetCaseDerivations(Complaint complaint)
        {
            foreach (var caseDeriv in complaint.CaseDerivation)
            {
                if (chkDerivJudge.Content.ToString() == caseDeriv.Institution)
                    chkDerivJudge.IsChecked = true;
                if (chkDerivPublicMinistery.Content.ToString() == caseDeriv.Institution)
                    chkDerivPublicMinistery.IsChecked = true;
                if (chkDerivFELCC.Content.ToString() == caseDeriv.Institution)
                    chkDerivFELCC.IsChecked = true;
                if (chkDerivDefensory.Content.ToString() == caseDeriv.Institution)
                    chkDerivDefensory.IsChecked = true;
                if (chkDerivOther.Content.ToString() == caseDeriv.Institution)
                    chkDerivOther.IsChecked = true;
            }
        }

        private void btnContinue_Click(object sender, RoutedEventArgs e)
        {
            if (viewComplaintpart2 == null)
                viewComplaintpart2 = new ViewComplaintPart2(this);
            var par = this.Parent as ItemsControl;
            par.Items.Clear();
            var scrollView = (par.Parent as ItemsControl).Parent as ScrollViewer;
            scrollView.ScrollToTop();
            par.Items.Add(viewComplaintpart2);
        }

        
    }
}
